package com.tledu.instrument_use.controller;

import com.tledu.common.annotation.Log;
import com.tledu.common.core.controller.BaseController;
import com.tledu.common.core.domain.AjaxResult;
import com.tledu.common.core.page.TableDataInfo;
import com.tledu.common.enums.BusinessType;
import com.tledu.common.utils.poi.ExcelUtil;
import com.tledu.instrument.domain.InstrumentUse;
import com.tledu.instrument.service.IInstrumentUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仪器使用记录Controller
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@RestController
@RequestMapping("/instrument_use/use")
public class InstrumentUseController extends BaseController {
    @Autowired
    private IInstrumentUseService instrumentUseService;


    /**
     * 查询仪器使用记录列表
     */
    @PreAuthorize("@ss.hasPermi('instrument_use:use:list')")
    @GetMapping("/list")
    public TableDataInfo list(InstrumentUse instrumentUse) {
        startPage();
        List<InstrumentUse> list = instrumentUseService.selectInstrumentUseList(instrumentUse);
        return getDataTable(list);
    }

    /**
     * 导出仪器使用记录列表
     */
    @PreAuthorize("@ss.hasPermi('instrument_use:use:export')")
    @Log(title = "仪器使用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InstrumentUse instrumentUse) {
        List<InstrumentUse> list = instrumentUseService.selectInstrumentUseList(instrumentUse);
        ExcelUtil<InstrumentUse> util = new ExcelUtil<InstrumentUse>(InstrumentUse.class);
        util.exportExcel(response, list, "仪器使用记录数据");
    }

    /**
     * 获取仪器使用记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('instrument_use:use:query')")
    @GetMapping( "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(instrumentUseService.selectInstrumentUseById(id));
    }

    /**
     * 新增仪器使用记录
     *
     * @param id 仪器id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('instrument_use:use:add')")
    @Log(title = "仪器使用记录", businessType = BusinessType.INSERT)
    @PostMapping("/{id}")
    public AjaxResult add(@PathVariable Long id) {
        int rows = instrumentUseService.insertInstrumentUse(id);
        return toAjax(rows);
    }

    /**
     * 修改仪器使用记录
     */
    @PreAuthorize("@ss.hasPermi('instrument_use:use:edit')")
    @Log(title = "仪器使用记录", businessType = BusinessType.UPDATE)
    @GetMapping
    public AjaxResult edit(@RequestBody InstrumentUse instrumentUse) {
        return toAjax(instrumentUseService.updateInstrumentUse(instrumentUse));
    }

    // 逻辑删除按钮
//    @PreAuthorize("@ss.hasPermi('instrument_use:use:edit')")
//    @Log(title = "仪器使用记录", businessType = BusinessType.UPDATE)
//    @PutMapping("/dellogic")
//    public AjaxResult deleteLogic(@RequestBody InstrumentUse instrumentUse) {
//        return toAjax(instrumentUseService.updateInstrumentStatus(instrumentUse));
//    }

    /**
     * 删除仪器使用记录
     */
//    @PreAuthorize("@ss.hasPermi('instrument_use:use:remove')")
//    @Log(title = "仪器使用记录", businessType = BusinessType.DELETE)
//    @PutMapping
//    public AjaxResult remove(@RequestBody InstrumentUse instrumentUse) {
//        return toAjax(instrumentUseService.deleteInstrumentUseByIds(instrumentUse));
//    }

    /**
     * 修改仪器使用状态
     */
    // 入库操作
    @PreAuthorize("@ss.hasPermi('instrument_use:use:edit')")
    @Log(title = "仪器使用记录", businessType = BusinessType.UPDATE)
    @PutMapping("/updatestatus")
    public AjaxResult upStatus(@RequestBody InstrumentUse instrumentUse) {
        return toAjax(instrumentUseService.updateStatus(instrumentUse));
    }

}
